package com.google.code.p.restrpc.spring.metadata;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.annotation.AnnotationBeanUtils;

import com.google.code.p.restrpc.annotation.RemoteService;
import com.google.code.p.restrpc.spring.InvalidMetadataException;

public class AnnotationRpcAttributeSource implements RpcAttributeSource {
    
    public RpcMetadata getRpcMetadata(Class beanClass) throws InvalidMetadataException {
        RemoteService ann = ((Class<?>) beanClass).getAnnotation(RemoteService.class);
        if (ann == null) {
            return null;
        }
        RpcMetadata rpcMetadata = new RpcMetadata();
        AnnotationBeanUtils.copyPropertiesToBean(ann, rpcMetadata);
        if (!"".equals(ann.value()) && !StringUtils.isNotEmpty(rpcMetadata.getServiceName())) {
            rpcMetadata.setServiceName(ann.value());
        }
        return rpcMetadata;
    }
    
}
